package com.djx.redis.utils;

import com.djx.comm.util.SerializeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisCluster;

import javax.annotation.PostConstruct;
import java.util.concurrent.TimeUnit;

/**
 * redis集群工具类
 * @author dingjunxiong
 */
@Component
public class JedisUtils {
    protected static Logger logger = LoggerFactory.getLogger(JedisUtils.class);
    private static JedisCluster jCluster;
    private static RedisTemplate<Object, Object> redisTemplate;
    private static Boolean isCluster = Boolean.FALSE;

    @Autowired
    protected JedisCluster jedisCluster;

    @Autowired
    protected RedisTemplate<Object, Object> redisTemplates;


    @PostConstruct
    public void init() {
        if(this.jedisCluster !=  null && jedisCluster.getClusterNodes().size() > 0) {
            jCluster = this.jedisCluster;
            isCluster = Boolean.TRUE;
        } else {
            redisTemplate = this.redisTemplates;
            isCluster = Boolean.FALSE;
        }
    }

    public static String set(String key, String value, int seconds) {
        String set = "";
        if (isCluster) {
            set = jCluster.set(key, value);
        } else {
            redisTemplate.opsForValue().set(key, value);
        }
        if (seconds > 0) {
            expire(key, seconds);
        }
        return set;
    }

    public static String get(String key) {
        String str = "";
        if (isCluster) {
            str = jCluster.get(key);
        } else {
            str = (String) redisTemplate.opsForValue().get(key);
        }
        return str;
    }

    public static Object getObject(String key) {
        if (isCluster) {
            byte[] bytes = jCluster.get(key.getBytes());
            return SerializeUtil.deserialize(bytes);
        } else {
            return redisTemplate.opsForValue().get(key);
        }
    }

    public static String setObject(String key, Object object, int seconds) {
        String set = "";
        if (isCluster) {
            set = jCluster.set(key.getBytes(), SerializeUtil.serizlize(object));
            if (seconds > 0) {
                expire(key, seconds);
            }
        } else {
            redisTemplate.opsForValue().set(key, object, seconds);
        }
        return set;
    }

    public static Boolean exists(String key) {
        Boolean exists = Boolean.FALSE;
        if (isCluster) {
            exists = jCluster.exists(key);
        } else {
            exists = redisTemplate.hasKey(key);
        }
        return exists;
    }

    public static Boolean expire(String key, int seconds) {
        Boolean bool = Boolean.FALSE;
        if (isCluster) {
            Long expire = jCluster.expire(key, seconds);
            if (expire > 0) {
                bool = Boolean.TRUE;
            }
        } else {
            bool = redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
        }
        return bool;
    }

    public static Boolean del(String key) {
        Boolean bool = Boolean.FALSE;
        if (isCluster) {
            Long del = jCluster.del(key);
            if (del > 0) {
                bool = Boolean.TRUE;
            }
        } else {
            bool = redisTemplate.delete(key);
        }
        return bool;
    }

    public static Long ttl(String key) {
        Long ttl;
        if (isCluster) {
            ttl = jCluster.ttl(key);
        } else {
            ttl = redisTemplate.opsForValue().getOperations().getExpire(key);
        }
        return ttl;
    }
}
